<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page import="com.onlineauction.helpers.Constants"%>
<%@ page import="com.onlineauction.entities.Item"%>
<%@ page import="com.onlineauction.entities.Bid"%>
<%@ page import="com.onlineauction.entities.Auction"%>
<%@ page import="com.onlineauction.helpers.Constants.Category"%>
<%@ page import="com.onlineauction.helpers.Constants.Usertype"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- 
	<link rel="stylesheet" href="style.css" />  
-->
<title>Online Auction Homepage</title>
<link rel="stylesheet"
	href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<link rel="stylesheet" href="style.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script src="timepicker_addon.js"></script>
</head>
<jsp:useBean id="homeBean" class="com.onlineauction.beans.HomeBean"
	scope="session" />
<%
	// redirect to Home if session is not set (user is not logged in yet)
	String username = (String) session.getAttribute(Constants.USERNAME);
	if (username == null) {
		request.getRequestDispatcher("/Home").forward(request, response);
	} else {
		homeBean.setUsername(username);
	}
%>


<body>
	<%
		String message = (String) request.getAttribute(Constants.MESSAGE);
		if (message != null && message.length() != 0) {
			out.println(message);
		}
	%>


	<!--The following HTML is shown when user logged in as a Seller.-->
	<%
		if (homeBean.getUser().getUsertype() == Usertype.Seller) {
	%>
	<div class="wrapper">
		<div id="Content">
			<div id="Header">
				<H1>
					Hello
					<%=(String) session.getAttribute(Constants.USERNAME)%>, welcome to
					seller's home.
				</H1>
				<div id="LogOutButtonDiv" align="left">
					<a href="LogoutAccount">Logout</a>
					<div align="right"><a href="Unsubscribe">Unsubscribe Your Account</a></div>
				</div>
			</div>
			<hr />
			<div id="MainContent">				
				<h3>Your items:</h3>
				<table>
					<tr>
						<th>Name</th>
						<th>Category</th>
						<th>Link</th>
					</tr>
					<%
						for (Item item : homeBean.getMyItems()) {
					%>
					<tr>
						<td><%=item.getProductName()%></td>
						<td><%=item.getCategory().toString()%></td>
						<td><button>
								<a
									href="/manageAuction.jsp?<%=Constants.ITEM_ID%>=<%=item.getItemID().toString()%>">Manage
									Auctions</a></td>
					</tr>
					<%
						}
					%>
					<tr>
					    <td/><td/>
					    <td><button><a href="/addItem.jsp">Add a New Item</a></button></td>
					    
					</tr>
				</table>
				<iframe src="<%="/review?reviewee=" + username%>" width="100%"
					height="680px"></iframe>
			</div>
		</div>
	</div>


	<!--The following HTML is shown when user logged in as a Buyer.-->
	<%
		} else {
	%>
	<div class="wrapper">
		<div id="Content">
			<div id="Header">
				<H1>
					Hello
					<%=(String) session.getAttribute(Constants.USERNAME)%>, welcome to
					buyer's home!
				</H1>
				<div id="LogOutButtonDiv" align="left">
					<a href="LogoutAccount">Logout</a>
					<div align="right"><a href="Unsubscribe">Unsubscribe Your Account</a></div>
				</div>
			</div>
			<hr />
			<div id="MainContent">
				<h3>Looking for something new? Start searching from here:</h3>
				<form action="/makeBid.jsp" method="get">
					<table>
						<tr>
							<td align="right">Item name: <input type="text"
								name="<%=Constants.PRODUCT_NAME%>"> (leave blank to see
								everything)
							</td>
							<td align="right">Category: <select
								name=<%=Constants.CATEGORY%> size="1">
									<option selected value="<%=Constants.ALL%>"><%=Constants.ALL%></option>
									<%
										Category[] categories = Constants.Category.values();
											for (Category c : categories) {
									%>
									<option value="<%=c.toString()%>"><%=c.toString()%></option>
									<%
										}
									%>
							</select></td>
							<td><input type="submit" value="Search" /></td>
						</tr>
					</table>
				</form>
				<h3>Recommendations for You:</h3>
				<table>
					<tr>
						<th>Item Name</th>
						<th>Category</th>
						<th>Seller</th>
						<th>Base Price</th>
						<th>Best Price</th>
						<th>Auction Open?</th>
						<th>Action</th>
					</tr>
					<%
						for (Auction auction : homeBean.getRecommend(username, 3)) {
								Item item = new Item(auction.getItemID());
								String itemName = item.getProductName();
								String sellerName = item.getOwner();
								String basePrice = String.format("CAD$ %.2f", auction.getAskPrice());
								Long bestBidID = Bid.getBestBidIDOfAuction(auction.getAuctionID());
								String category = item.getCategory().toString();
								String bestPrice = "";
								if (bestBidID != null) {
									bestPrice = String.format("CAD$ %.2f", new Bid(bestBidID).getBidPrice());
								}
					%>
					<tr>
						<td><button><a href="ConfirmBid?<%=Constants.ITEM_ID%>=<%=item.getItemID()%>"><%=itemName%></a></td>
						<td><%=category%></td>
						<td><button><a href="<%="/review?reviewee=" + sellerName%>"><%=sellerName%></a></button></td>
						<td><%=basePrice%></td>
						<td><%=bestPrice%></td>
						<td><button><a href="/ViewAuction?<%=Constants.AUCTION_ID%>=<%=auction.getAuctionID()%>">Open</a></button></td>						
						<td><button><a href="/ConfirmBid?<%=Constants.ITEM_ID%>=<%=item.getItemID()%>">Bid This!</a></button>													
					</tr>
					<%
						}
					%>
				</table>
				<h3>The bids you have made before:</h3>
				<table>
					<tr>
						<th>Item Name</th>
						<th>Seller</th>
						<th>Auction Status</th>
						<th>Bid Price</th>
						<th>Bid Time</th>
						<th>Bid Status</th>
					</tr>
					<%
						for (Bid bid : homeBean.getMyBids()) {
								Auction auction = new Auction(bid.getAuctionID());
								Item item = new Item(auction.getItemID());
								String auctionStatus = null;
								if (auction.isActive()) {
									auctionStatus = "Auction Active";
								} else {
									auctionStatus = "Expired/Canceled";
								}

								Long bestBidID = Bid.getBestBidIDOfAuction(auction.getAuctionID());
								String bidStatus = null;
								if (bid.getBidID() == bestBidID) {
									bidStatus = "This is the best bid";
								} else {
									bidStatus = "This bid was outbid";
								}
					%>
					<tr>
						<td><button><a
							href="/ConfirmBid?<%=Constants.ITEM_ID%>=<%=item.getItemID()%>"><%=item.getProductName()%></a></button></td>
						<td><button><a href="<%="/review?reviewee=" + item.getOwner()%>"><%=item.getOwner()%></a></button></td>						
						<td><%=auctionStatus%></td>
						<td><%=String.format("%.2f", bid.getBidPrice())%>
						<td><%=bid.getBidTime().toString()%>						
						<td><button><a href="/ViewAuction?<%=Constants.AUCTION_ID%>=<%=bid.getAuctionID()%>"><%=bidStatus%></a></button></td>
					</tr>
					<%
						}
					%>
				</table>
			</div>
		</div>
	</div>
	<%
		}
	%>


</body>
</html>
